/**版本1.0,生成时间:2017-04-17 14:54:43**/
package com.xbdun.dao.bmp.mapper;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

import com.xbdun.dao.bmp.entity.XbdunRoleDo;
import com.xbdun.dao.bmp.provider.RoleSqlProvider;

/**
 * 角色
 * @author 张国涛
 * @2017-04-17 14:54:43
 */
@Mapper
public interface RoleMapper{
	
	/**
	 * 判断是否存在,不为0则是存在
	 * @param param
	 * @return int
	 */
	@Select("SELECT COUNT(*) FROM xbdun_role WHERE orgCode IN (#{orgCode},#{xbdunOrgCode}) AND roleName = #{roleName}")
	public int existRoleInOrg(@Param("orgCode")Long orgCode, @Param("xbdunOrgCode")Long xbdunOrgCode, @Param("roleName")String roleName);
	
	/**
	 * 新增
	 * @param XbdunRoleDo 角色
	 */
	@Insert("INSERT INTO xbdun_role(orgCode,roleName,roleShort,createTime,optionUser,version) VALUES(#{a.orgCode},#{a.roleName},#{a.roleShort},#{a.createTime},#{a.optionUser},#{a.version})")
	@SelectKey(statement="SELECT LAST_INSERT_ID() AS ID", before = false, keyProperty = "a.id", resultType = Long.class)
	public Long addXbdunRole(@Param("a")XbdunRoleDo xbdunRoleDo);
	
	/**
	 * 查询要修改的角色的版本号
	 * @param id
	 * @return Timestamp
	 */
	@Select("SELECT version FROM xbdun_role where id=#{id}")
	public Timestamp getRoleVersion(@Param("id")long id);
	
	/**
	 * 动态更新参数,参数中Map可以必须跟Key为Where的更新条件<br/>
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> setting = new HashMap<String,Object>();
	 *  setting.put("email", "360730440@qq.com");
	 *
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  where.put("userId", "1222");
	 * </pre>
	 * Map的key值就是数据库字段<br/>
	 * Map的value就是值
	 */
	@UpdateProvider(type=RoleSqlProvider.class,method="updateDynXbdunRoleSql")
	public void updateDynXbdunRole(@Param("param")Map<String,Object> param,@Param("_where")Map<String,Object> where);

	/**
	 * 根据主键获取对象
	 * @return
	 */
	@Select("SELECT * FROM xbdun_role where id=#{id}")
	public XbdunRoleDo getXbdunRoleById(@Param("id")long id);
	
	/**
	 * 删除数据根据主键
	 * @param id
	 */
	@Delete("DELETE FROM xbdun_role WHERE id = #{id}")
	public void delXbdunRoleById(@Param("id")long id);
	
	//------------------------//start分页查询--------------------------------------
	/**
	 * 分页查询 
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param _where where条件，现在支持And条件类型
	 * @param orderBy 排序条件
	 * @param pageNum 当前索引页
	 * @param numPerPage 每页显示条数
	 * @return
	 */
	@SelectProvider(type=RoleSqlProvider.class,method="queryPagerSql")
	public List<XbdunRoleDo> queryPager(@Param("_where")Map<String,Object> where,@Param("_orderby")String orderBy,@Param("pageIndex")int pageNum,@Param("pageSize")int numPerPage);
	
	/**
	 * 分页查询返回总数量
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param where 参数为where约束参数
	 * @return
	 */
	@SelectProvider(type=RoleSqlProvider.class,method="queryPagerCountSql")
	public Long queryPagerCount(@Param("_where")Map<String,Object> where);
	//-------------------------//end--------------------------------------------

	/**
	 * 通过角色id查询所属机构
	 * @param id
	 * @return
	 */
	@Select("SELECT orgCode FROM xbdun_role WHERE id = #{id}")
	public Long getOrgCodeById(@Param("id")long id);

	/**
	 * 通过角色id删除当前角色
	 * @param id
	 * @return
	 */
	@Delete("DELETE FROM xbdun_role WHERE id = #{id} AND version = #{version}")
	public Long deleteRole(@Param("id")Long id, @Param("version")Timestamp version);

	/**
	 * 通过角色id获取角色的版本号
	 * @param id
	 * @return
	 */
	@Select("SELECT version FROM xbdun_role WHERE id = #{id}")
	public Timestamp getVersionById(@Param("id")Long id);
}